package com.burntsky.xbmc.android;


import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Button;

import org.xbmc.android.remote.business.Command;
import org.xbmc.android.remote.business.ManagerFactory;
import org.xbmc.android.remote.business.TvShowManager;
import org.xbmc.android.util.ClientFactory;
import org.xbmc.android.util.HostFactory;
import org.xbmc.api.business.DataResponse;
import org.xbmc.api.business.INotifiableManager;
import org.xbmc.api.business.ITvShowManager;
import org.xbmc.api.object.Host;
import org.xbmc.api.object.TvShow;
import org.xbmc.api.presentation.INotifiableController;
import org.xbmc.httpapi.*;

import com.burntsky.xbmc.android.TvShowListFragment.OnTvShowSelectedListener;

public class HomeActivityBase extends Activity  {
	
	//Button mMyButton;
	protected XbmcController _Controller;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //this.setContentView(R.layout.main);
        
        this._Controller = new XbmcController(this.getApplicationContext());
        
        ActionBar bar = getActionBar();
        
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        TvShowListFragment t1f = new TvShowListFragment(this._Controller);
        bar.addTab(bar.newTab().setText(this.getString(R.string.Navigation_TvShows)).setTabListener(new TabListener(t1f)));       

        MovieListFragment t2f = new MovieListFragment(this._Controller);
        bar.addTab(bar.newTab().setText(this.getString(R.string.Navigation_Movies)).setTabListener(new TabListener(t2f)));
  

/*
        this.mMyButton = ((Button)this.findViewById(R.id.button_home_test));
        this.mMyButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				new AsyncTask<String, Void, String>(){
					@Override
					protected void onPostExecute(String result) {
						HomeActivity.this.mMyButton.setText(result);
					}
					@Override
					protected String doInBackground(String... arg0) {
						// TODO Auto-generated method stub
						return "adsfasdfasdf";
					}
				}.execute("value1");
			}
		});
		*/
    }
    /*
    private class MyTestAsyncTask extends AsyncTask<Void, Void, String> {
    	
        protected String doInBackground(Void...voids) {
           
        	
			
			return shows.get(0).title;
			//api.info.getSystemInfo(manager, field)
			//return api.control.getCurrentlyPlaying(mgr).getFilename();
			
        	
        	
        }
        
        public class TestNotifiableController implements INotifiableController
        {
        	Context context;
        	public TestNotifiableController(Context context)
        	{
        		this.context = context;
        	}

			@Override
			public void onWrongConnectionState(int state,
					INotifiableManager manager, Command<?> source) {
				// TODO Auto-generated method stub

				//Utils.Alert(HelloAndroid.this, "onWrongConnectionState");
			}

			@Override
			public void onError(Exception e) {
				// TODO Auto-generated method stub
				// HelloAndroid.this.mMyButton.setText(e.getMessage());
				Utils.Alert(context, e.getMessage());
			}

			@Override
			public void onMessage(String message) {
				// TODO Auto-generated method stub
				//Utils.Alert(HelloAndroid.this, message);
				 HelloAndroid.this.mMyButton.setText(message);
			}

			@Override
			public void runOnUI(Runnable action) {
				// TODO Auto-generated method stub

				//Utils.Alert(HelloAndroid.this, "runOnUI");
			}
        	
        }

        protected void onPostExecute(String result) {
            HelloAndroid.this.mMyButton.setText(result);
        	//Utils.Alert(HelloAndroid.this, result);
        }
    }
    */
    /* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		//return super.onCreateOptionsMenu(menu);
		
		MenuInflater mi = this.getMenuInflater();
		mi.inflate(R.menu.main_menu, menu);
		
		//menu.add("menu 1").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
		return true;
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in Action Bar clicked; go home
                Intent intent = new Intent(this, HomeActivityBase.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            case R.id.menu_about:
            	this.showNotification();
            	return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
	

    void showNotification() {
        final Resources res = getResources();
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_notify_example)
                .setAutoCancel(true)
                .setTicker("ticker text")
                .setContentIntent(getDialogPendingIntent("Tapped the notification entry."));

            builder
                    .setNumber(7) // An example number.
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("content text");

        notificationManager.notify(1, builder.getNotification());
    }
	

    PendingIntent getDialogPendingIntent(String dialogText) {
        return PendingIntent.getActivity(
                this,
                dialogText.hashCode(), // Otherwise previous PendingIntents with the same
                                       // requestCode may be overwritten.
                new Intent("com.xxx")
                        .putExtra(Intent.EXTRA_TEXT, dialogText)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                0);
    }
    
    public class TabListener implements ActionBar.TabListener
    {
    	private Fragment frag;
    	
    	public TabListener(Fragment fragment)
    	{
    		this.frag = fragment;
    	}
    	
    	public void onTabReselected(Tab tab, FragmentTransaction ft) {
    		// TODO Auto-generated method stub
     	}

    	public void onTabSelected(Tab tab, FragmentTransaction ft) {
    		// TODO Auto-generated method stub
       		ft.add(R.id.left_pane, this.frag);
    	}

    	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    		// TODO Auto-generated method stub
    		ft.remove(this.frag);
    	}
    }



}

